package icursoft.educationms;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2016/12/21.
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    View view;
    Context mContext;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (getLayoutId() != 0) {
            return view = inflater.inflate(getLayoutId(), null);
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();
//        EventBus.getDefault().register(this);
        setupViews();
        initialized();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void HttpError(EventRefreshFragment refresh) {
//        if (refresh.isRefresh()) {
//            onTokenShortSuccessLogicAgain();
//        }
//    }

    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    protected abstract int getLayoutId();

    protected abstract void setupViews();

    protected abstract void initialized();

    public <T extends View> T getView(int rid) {
        return (T) view.findViewById(rid);
    }

    /**
     * 短Token失效调用的抽象方法
     */
    public abstract void onTokenShortSuccessLogicAgain();

}
